import 'dart:io';

import 'package:flutter/material.dart';
import 'package:video_player/video_player.dart';
import 'package:yuan_hang_app/base/full_base_page.dart';
import 'package:yuan_hang_app/res/w_colors.dart';
import 'package:yuan_hang_app/res/w_dimens.dart';
import 'package:yuan_hang_app/utils/size_utils.dart';
import 'package:yuan_hang_app/utils/string_utils.dart';
import 'package:yuan_hang_app/widget/w_text.dart';

class VideoPreview extends StatefulWidget {
  final String url;

  const VideoPreview({Key? key, required this.url}) : super(key: key);

  @override
  _VideoPreviewState createState() => _VideoPreviewState();
}

class _VideoPreviewState extends State<VideoPreview> {
  VideoPlayerController? _videoPlayerController;
  bool _isPlaying = false;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if (widget.url.contains("http")) {
      _videoPlayerController = VideoPlayerController.network(widget.url)
        ..initialize().then((_) {
          _videoPlayerController!.play();
          print("====================初始化成功..........");
          setState(() {});
        });
    } else {
      _videoPlayerController = VideoPlayerController.file( File(widget.url))
        ..initialize().then((_) {
          _videoPlayerController!.play();
          print("====================初始化成功..........");
          setState(() {});
        });
    }

    _videoPlayerController!.addListener(() {
      setState(() {
        _isPlaying = _videoPlayerController!.value.isPlaying;
      });
    });
  }

  @override
  void dispose() {
    _videoPlayerController?.dispose();
    // TODO: implement dispose
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return FullBasePage(
      title: "视频查看",
      backgroundColor: Colors.black,
      child: Stack(
        children: [
          _videoPlayerController!.value.isInitialized
              ? Center(
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      InkWell(
                        onTap: () {
                          if (_isPlaying) {
                            _videoPlayerController!.pause();
                          }
                        },
                        child: Container(
                          constraints: BoxConstraints(
                              maxWidth: SizeUtils.screenW(),
                              maxHeight: SizeUtils.screenH() * 0.8),
                          child: AspectRatio(
                            aspectRatio:
                                _videoPlayerController!.value.aspectRatio,
                            child: VideoPlayer(_videoPlayerController!),
                          ),
                        ),
                      ),
                      Row(
                        children: [
                          Expanded(
                            child: SliderTheme(
                              data: SliderTheme.of(context).copyWith(
                                  activeTrackColor:
                                      Colors.white.withOpacity(0.5),
                                  //进度条滑块左边颜色
                                  inactiveTrackColor:
                                      Colors.white.withOpacity(0.2),
                                  //进度条滑块右边颜色
                                  thumbColor: Colors.transparent,
                                  //滑块颜色
                                  overlayColor: Colors.transparent,
                                  //滑块拖拽时外圈的颜色
                                  trackHeight: 2.px,
                                  //进度条宽度
                                  thumbShape: RoundSliderThumbShape(
                                      enabledThumbRadius: 10.px)),
                              child: Slider(
                                max: _videoPlayerController!
                                    .value.duration.inMilliseconds
                                    .truncateToDouble(),
                                value: _videoPlayerController!
                                    .value.position.inMilliseconds
                                    .truncateToDouble(),
                                onChanged: (double value) {
                                  _videoPlayerController!.seekTo(
                                    Duration(
                                      milliseconds: value.truncate(),
                                    ),
                                  );
                                },
                              ),
                            ),
                          ),
                          WText(
                            StringUtils.secToTime(_videoPlayerController!
                                .value.duration.inMilliseconds),
                            style: TextStyle(
                                color: WColors.white,
                                fontSize: WDimens.tsLabel),
                          ),
                          SizedBox(
                            width: 10.px,
                          )
                        ],
                      )
                    ],
                  ),
                )
              : Center(
                  child: CircularProgressIndicator(
                    valueColor:
                        AlwaysStoppedAnimation(Colors.white.withOpacity(0.7)),
                    backgroundColor: Colors.white.withOpacity(0.4),
                  ),
                ),
          if (_videoPlayerController!.value.isInitialized && !_isPlaying)
            Center(
              child: GestureDetector(
                onTap: () {
                  _videoPlayerController!.play();
                },
                child: Icon(
                  Icons.play_circle_fill_outlined,
                  color: Colors.white,
                  size: 60.px,
                ),
              ),
            )
        ],
      ),
    );
  }
}
